package com.wss.llm.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wss.llm.configuration.ReqParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@Service
@Slf4j
public class DeepSeekClient {
    @Autowired
    private ReqParam reqParam;
    public  String sendRequest(String prompt) throws Exception {
        log.info("prompt >>>"+prompt);
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(reqParam.getApiUrl());
            
            // 设置 Header
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setHeader("Authorization", "Bearer " + reqParam.getApiKey());
            
            // 构建请求体
            String jsonBody = String.format("{\"model\": \"%s\", \"messages\": [{\"role\": \"user\", \"content\": \"%s\"}]}",reqParam.getApiModel(), prompt);
            httpPost.setEntity(new StringEntity(jsonBody, StandardCharsets.UTF_8));
            
            // 执行请求
            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                HttpEntity entity = response.getEntity();
                ObjectMapper mapper = new ObjectMapper();

                try {
                    // 解析 JSON 字符串
                    JsonNode rootNode = mapper.readTree(EntityUtils.toString(entity));
                    // 获取 "choices" 节点
                    JsonNode choicesNode = rootNode.get("choices");

                    if (choicesNode != null && choicesNode.isArray()) {
                        for (JsonNode choice : choicesNode) {
                            // 获取 "message" 节点
                            JsonNode messageNode = choice.get("message");
                            if (messageNode != null) {
                                // 提取 role 和 content
                                String role = messageNode.get("role").asText();
                                String content = messageNode.get("content").asText();
                                log.info("content >>>"+content);
                                return content;
                            }
                        }
                    } else {
                        log.info("没有找到 'choices' 数组节点！");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                log.info("entity>>>"+EntityUtils.toString(entity));
                return EntityUtils.toString(entity);
          }

      }
        }
}